package com.example.SocialApplication.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.example.SocialApplication.db.dao.ColorEntityDAO;
import com.example.SocialApplication.db.dao.ListThingsDAO;
import com.example.SocialApplication.db.dao.ListThingsToThingDAO;
import com.example.SocialApplication.db.dao.ThingDAO;
import com.example.SocialApplication.db.dao.ThingMainDAO;
import com.example.SocialApplication.db.entity.ColorEntity;
import com.example.SocialApplication.db.entity.ListThings;
import com.example.SocialApplication.db.entity.ListThingsToThing;
import com.example.SocialApplication.db.entity.Thing;
import com.example.SocialApplication.db.entity.ThingMain;
import com.example.SocialApplication.db.service.AddColorsService;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import java.sql.SQLException;

public class DBHelper extends OrmLiteSqliteOpenHelper {

    private static final String DATABASE_NAME = "stuffix.db";
    private static final int DATABASE_VERSION = 1;

    private ListThingsDAO listThingsDAO = null;
    private ThingDAO thingDAO = null;
    private ColorEntityDAO colorEntityDAO = null;
    private ListThingsToThingDAO listThingsToThingDAO = null;
    private ThingMainDAO thingMainDAO = null;

    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        Log.i(getClass().getName(), "DBHelper(Context context)   " + DATABASE_NAME);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {
        Log.i(getClass().getName(), "in onCreate DB  " + DATABASE_NAME);
        try
        {
            TableUtils.createTable(connectionSource, ListThings.class);
            TableUtils.createTable(connectionSource, Thing.class);
            TableUtils.createTable(connectionSource, ThingMain.class);
            TableUtils.createTable(connectionSource, ColorEntity.class);
            TableUtils.createTable(connectionSource, ListThingsToThing.class);
            fillData();
        }
        catch (SQLException e){
            Log.e(getClass().getName(), "error creating DB " + DATABASE_NAME);
            throw new RuntimeException(e);
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int i, int i2) {
        try{
            TableUtils.dropTable(connectionSource, ListThings.class, true);
            TableUtils.dropTable(connectionSource, Thing.class, true);
            TableUtils.dropTable(connectionSource, ThingMain.class, true);
            TableUtils.dropTable(connectionSource, ColorEntity.class, true);
            TableUtils.dropTable(connectionSource, ListThingsToThing.class, true);
            onCreate(sqLiteDatabase, connectionSource);
        }
        catch (SQLException e){
            Log.e(getClass().getName(),"error upgrading db "+DATABASE_NAME+" from ver "+i);
            throw new RuntimeException(e);
        }
    }

    private void fillData() {
        new AddColorsService().execute();
    }

    public ListThingsDAO getListThingsDAO() throws SQLException {
        if(listThingsDAO == null) {
            listThingsDAO = new ListThingsDAO(getConnectionSource(), ListThings.class);
        }
        return listThingsDAO;
    }

    public ThingDAO getThingDAO() throws SQLException {
        if(thingDAO == null) {
            thingDAO = new ThingDAO(getConnectionSource(), Thing.class);
        }
        return thingDAO;
    }

    public ThingMainDAO getThingMainDAO() throws SQLException {
        if(thingMainDAO == null) {
            thingMainDAO = new ThingMainDAO(getConnectionSource(), ThingMain.class);
        }
        return thingMainDAO;
    }

    public ColorEntityDAO getColorEntityDAO() throws SQLException {
        if(colorEntityDAO == null) {
            colorEntityDAO = new ColorEntityDAO(getConnectionSource(), ColorEntity.class);
        }
        return colorEntityDAO;
    }

    public ListThingsToThingDAO getListThingsToThingDAO() throws SQLException {
        if(listThingsToThingDAO == null) {
            listThingsToThingDAO = new ListThingsToThingDAO(getConnectionSource(), ListThingsToThing.class);
        }
        return listThingsToThingDAO;
    }

    @Override
    public void close() {
        super.close();
        listThingsDAO = null;
        thingDAO = null;
        thingMainDAO = null;
        colorEntityDAO = null;
        listThingsToThingDAO = null;
    }
}
